//
//  MKTitleBar.swift
//  MOKOAnnounce
//
//  Created by 曹相召 on 2017/7/7.
//  Copyright © 2017年 cc.moko. All rights reserved.
//

import UIKit

@objc protocol MKTitleBarProtocol:NSObjectProtocol {
    @objc optional func titleBarLeftClick() -> Void
    @objc optional func titleBarRightClick() -> Void
}

//按钮的枚举,根据需要扩展即可
enum MKTitleBarButtonStyle {
    case None
    case Back
    case Cancel
    case Register
    case Login
    case Send
    case Camera
    case Add
    case Account
    case Friend
    case Create
    case Upload
    case WhiteBack
    case WhiteMore
    case OK
    case More
    case Share
    case Setting

}
enum MKTitleBarStyle {
    case Default
    case Black
}


    class MKTitleBar: UIView {
        var leftButton:UIButton!
        var rightButton:UIButton!
        var titleLabel:UILabel!
        weak var delegate:MKTitleBarProtocol?
        
        func titleBar(title:String,leftStyle:MKTitleBarButtonStyle,rightStyle:MKTitleBarButtonStyle,barStyle:MKTitleBarStyle) ->MKTitleBar{
        let titleBar:MKTitleBar = MKTitleBar.init(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth, height: 64))
        
        titleBar.backgroundColor = UIColorFromRGB(rgbValue: 0xf8f8f8, A: 1)
        titleBar.layer.shadowOffset = CGSize.init(width: 0, height: 1)
        titleBar.layer.shadowOpacity = 0.1
        
        titleBar.leftButton = UIButton.init(frame: CGRect.init(x: 0, y: 20, width: 50, height: 44))
        titleBar.rightButton = UIButton.init(frame: CGRect.init(x: ScreenWidth - 50, y: 20, width: 50, height: 44))
        
        titleBar.titleLabel = UILabel.label(fontSize: 18, textColor: UIColorFromRGB(rgbValue: 0x2d2d2d, A: 1))
        titleBar.titleLabel?.frame = CGRect.init(x: 50, y: 20, width: ScreenWidth - 100, height: 44)
        titleBar.titleLabel?.textAlignment = .center
        titleBar.titleLabel?.backgroundColor = UIColor.clear
        titleBar.titleLabel?.text = title
       
        titleBar.leftButton.addTarget(self, action: #selector(MKTitleBar.leftButtonClick), for: .touchUpInside)
        titleBar.rightButton.addTarget(self, action: #selector(MKTitleBar.rightButtonClick), for: .touchUpInside)

        switch barStyle {
        case .Default:
        break
        case .Black:
            titleBar.backgroundColor = UIColorFromRGB(rgbValue: 0x000000, A: 1)
            titleBar.titleLabel?.textColor = UIColor.white
        break
        }
        
       titleBar.addSubview(titleBar.leftButton!)
       titleBar.addSubview(titleBar.rightButton!)
       titleBar.addSubview(titleBar.titleLabel!)
        
       self.setButtonStyle(button: titleBar.leftButton!, buttonStyle: leftStyle)
       self.setButtonStyle(button: titleBar.rightButton!, buttonStyle: rightStyle)

       return titleBar;
    }
    func leftButtonClick() -> Void {
        if((self.delegate) != nil && (self.delegate?.responds(to: #selector(MKTitleBarProtocol.titleBarLeftClick)))!){
            self.delegate?.titleBarLeftClick!();
        }
    }
    func rightButtonClick() -> Void {
        
        if((self.delegate) != nil && (self.delegate?.responds(to: #selector(MKTitleBarProtocol.titleBarRightClick)))!){
            self.delegate?.titleBarRightClick!();
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    func setButtonStyle(button:UIButton!,buttonStyle:MKTitleBarButtonStyle) -> Void {
        
        button.isHidden = false
        
        switch buttonStyle {
        case .None:
            button.isHidden = true
            break
        case .Back:
            button.setImage(UIImage.init(named: "titlebar-back"), for: .normal)
            break
            
        case .Setting:
            button.setImage(UIImage.init(named: "newMe_setting"), for: .normal)
            break
    
        default:
            button.isHidden = true
            break
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
